import pika
import json
from config import RABBITMQ_CONFIG

QUEUE_PREFIX = RABBITMQ_CONFIG['queue_prefix']

EXCHANGE_NAME = 'demo_topic'
EXCHANGE_TYPE = 'topic'
QUEUE_NAME = QUEUE_PREFIX + 'ws_consumer'
ROUTING_KEY = 'default'

MULTITASK_EXCHANGE_NAME = 'multitask_topic'
MULTITASK_QUEUE_NAME = QUEUE_PREFIX + 'ws_multitask_consumer'

print ('RABBITMQ_CONFIG = ', RABBITMQ_CONFIG)

def init_singletask_rabbitmq():
    credentials = pika.PlainCredentials(RABBITMQ_CONFIG['username'], RABBITMQ_CONFIG['password'])
    parameters = pika.ConnectionParameters(
        host=RABBITMQ_CONFIG['host'],
        port=RABBITMQ_CONFIG['port'],
        credentials=credentials
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    # 创建主题交换机
    channel.exchange_declare(exchange=EXCHANGE_NAME, exchange_type=EXCHANGE_TYPE, durable=True)

    # 创建默认队列
    channel.queue_declare(queue=QUEUE_NAME, durable=True)
    # 绑定队列到交换机，routing key '#'
    channel.queue_bind(queue=QUEUE_NAME, exchange=EXCHANGE_NAME, routing_key='#')

    connection.close()

def init_multitask_rabbitmq():
    credentials = pika.PlainCredentials(RABBITMQ_CONFIG['username'], RABBITMQ_CONFIG['password'])
    parameters = pika.ConnectionParameters(
        host=RABBITMQ_CONFIG['host'],
        port=RABBITMQ_CONFIG['port'],
        credentials=credentials
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    # 创建主题交换机
    channel.exchange_declare(exchange=MULTITASK_EXCHANGE_NAME, exchange_type=EXCHANGE_TYPE, durable=True)

    # 创建默认队列
    channel.queue_declare(queue=MULTITASK_QUEUE_NAME, durable=True)
    # 绑定队列到交换机，routing key '#'
    channel.queue_bind(queue=MULTITASK_QUEUE_NAME, exchange=MULTITASK_EXCHANGE_NAME, routing_key='#')

    connection.close()

def init_rabbitmq():
    init_singletask_rabbitmq()
    init_multitask_rabbitmq()    

def send_to_rabbitmq(message):
    credentials = pika.PlainCredentials(RABBITMQ_CONFIG['username'], RABBITMQ_CONFIG['password'])
    parameters = pika.ConnectionParameters(
        host=RABBITMQ_CONFIG['host'],
        port=RABBITMQ_CONFIG['port'],
        credentials=credentials
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.basic_publish(
        exchange=EXCHANGE_NAME,
        routing_key=ROUTING_KEY,
        body=json.dumps(message),
        properties=pika.BasicProperties(delivery_mode=2)
    )
    connection.close() 
    print("发送了一条数据到rabbitmq")

def send_to_multitask_rabbitmq(message):
    credentials = pika.PlainCredentials(RABBITMQ_CONFIG['username'], RABBITMQ_CONFIG['password'])
    parameters = pika.ConnectionParameters(
        host=RABBITMQ_CONFIG['host'],
        port=RABBITMQ_CONFIG['port'],
        credentials=credentials
    )
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.basic_publish(
        exchange=MULTITASK_EXCHANGE_NAME,
        routing_key=ROUTING_KEY,
        body=json.dumps(message),
        properties=pika.BasicProperties(delivery_mode=2)
    )
    connection.close() 